{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook you can define your own configuration and run the model based on your custom configuration."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`dataset_name` is the name of the dataset which will be used in the model. In case of using KITTI, `dataset_path` shows the path to `data_paths` directory that contains every image and its pair path, and for Cityscape it is the path to the directory that contains `leftImg8bit` and `rightImg8bit` folders. The `resize` value selects the width, and the height dimensions that each image will be resized to."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_name: 'KITTI'\n",
    "dataset_path = '.'\n",
    "resize = [128, 256]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`baseline_model` selects the compression model. The accepted models for this parameter are bmshj18 for [Variational image compression with a scale hyperprior](https://arxiv.org/abs/1802.01436) and bls17 for [End-to-end Optimized Image Compression](https://arxiv.org/abs/1611.01704). If `use_side_info` is set as `True`, then the baseline model is modified using our proposed method for using side information for compressing.\n",
    "If `load_weight` is `True`, then in model initialization, the weight saved in `weight_path` is loaded to the model. You can also specify the experiment name in `experiment_name`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "baseline_model = 'bls17' # can be bmshj18 for Variational image compression with a scale hyperprior by Ballé, et al.\n",
    "                          # or bls17 for End-to-end Optimized Image Compression by Ballé, et al.\n",
    "use_side_info = True # if True then the modified version of baseline model for distributed compression is used.\n",
    "num_filters = 192 # number of filters used in the baseline model network\n",
    "cuda = True\n",
    "load_weight = False\n",
    "weight_path = './pretrained_weights/ours+balle17_MS-SSIM_lambda3e-05.pt' # weight path for loading the weight\n",
    "# note that we provide some pretrained weights, accessible from the anonymous link provided in README.md"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For training set `train` to be `True`. `lambda` shows the lambda value in the rate-distortion equation and `alpha` and `beta` correspond to the handles on the reconstruction of the correlated image and amount of common information extracted from the decoder-only side information, respectively. `distortion_loss` selects the distortion evaluating method. Its accepted values are MS-SSIM for the ms-ssim method or MSE for mean squared error.\n",
    "`verbose_period: 50` indicates that every 50 epochs print the results of the validation dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = True\n",
    "epochs = 50000\n",
    "train_batch_size = 1\n",
    "lr = 0.0001\n",
    "lmbda = 0.00003 # the lambda value in rate-distortion equation\n",
    "alpha = 1\n",
    "beta = 1\n",
    "distortion_loss = 'MS-SSIM' # can be MS-SSIM or MSE. selects the method by which the distortion is calculated during training\n",
    "verbose_period = 50 # non-positive value indicates no verbose"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Weights and Results parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you wish to save the model weights after training set `save_weights` `True`. `save_output_path` shows the directory path where the model weights are saved.\n",
    "For the weights, in `save_output_path` a `weight` folder will be created, and the weights will be saved there with the name according to `experiment_name`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_weights = True\n",
    "save_output_path = './outputs' # path where results and weights will be saved\n",
    "experiment_name = 'bls17_with_side_info_MS-SSIM_lambda:3e-05'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you wish to test the model and save the results set `test` to `True`. If `save_image` is set to `True` then a `results` folder will be created, and the reconstructed images will be saved in `save_output_path/results` during testing, with the results named according to `experiment_name`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = True\n",
    "save_image = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to (only) carry out inference, please open `configs/config.yaml` and change the relevant lines as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resize = [128, 256] # we used this crop size for our inference\n",
    "dataset_path = '.'\n",
    "train = False\n",
    "load_weight = True\n",
    "test = True\n",
    "save_output_path = './inference' \n",
    "save_image = True "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download the desired weights and put them in `pretrained_weights` folder and put the dataset folder in the root . \n",
    "\n",
    "Based on the weight you chose, specify the weight name, and the experiment name in `configs/config.yaml`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "weight_path: './pretrained_weights/...'  # load a specified pre-trained weight\n",
    "experiment_name: '...' # a handle for the saved results of the inference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, change `baseline_model` and `use_side_info` parameters in `configs/config.yaml` accordingly.\n",
    "For example, for the `balle2017+ours` weights, these parameters should be: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "baseline_model: 'bls17'\n",
    "use_side_info: True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the code using the commands in below section, the results will be saved in `inference` folder."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving Custom Configuration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By running this piece of code you can save your configuration as a yaml file file in the configs folder. You can set your configuration file name by changing `config_name` variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "\n",
    "config = {\n",
    "    \"dataset_name\": dataset_name,\n",
    "    \"dataset_path\": dataset_path,\n",
    "    \"resize\": resize,\n",
    "    \"baseline_model\": baseline_model,\n",
    "    \"use_side_info\": use_side_info,\n",
    "    \"num_filters\": num_filters,\n",
    "    \"cuda\": cuda,\n",
    "    \"load_weight\": load_weight,\n",
    "    \"weight_path\": weight_path,\n",
    "    \"experiment_name\": experiment_name,\n",
    "    \"train\": train,\n",
    "    \"epochs\": epochs,\n",
    "    \"train_batch_size\": train_batch_size,\n",
    "    \"lr\": lr,\n",
    "    \"lambda\": lmbda,\n",
    "    \"distortion_loss\": distortion_loss,\n",
    "    \"verbose_period\": verbose_period,\n",
    "    \"save_weights\": save_weights,\n",
    "    \"save_output_path\": save_output_path,\n",
    "    \"test\": test,\n",
    "    \"save_image\": save_image\n",
    "}\n",
    "\n",
    "config_name = \"CUSTOM_CONFIG_FILE_NAME.yaml\"\n",
    "\n",
    "with open('configs/' + config_name) + config_name, 'w') as outfile:\n",
    "    yaml.dump(config, outfile, default_flow_style=None, sort_keys=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python main.py --config=configs/$config_name"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}